package com.quasar.hpatient.widget;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.quasar.hpatient.R;

public class MyView extends LinearLayout {

    private Drawable icon;
    private View container;
    private String title, num;
    private TextView titleview,numview;
    private ImageView iconView;

    public MyView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context, attrs, defStyleAttr);
    }

    public MyView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context, attrs, 0);
    }

    public void init(Context context, AttributeSet attrs, int defStyleAttr) {
        final TypedArray a = getContext().obtainStyledAttributes(
                attrs, R.styleable.MyView);
        title = a.getString(R.styleable.MyView_view_title);
        num = a.getString(R.styleable.MyView_view_num);
        if (a.hasValue(R.styleable.MyView_view_icon)) {
            icon = a.getDrawable(
                    R.styleable.MyView_view_icon);
            icon.setCallback(this);
        }
        a.recycle();
        LayoutInflater.from(context).inflate(R.layout.widget_my, this);
        titleview= (TextView) findViewById(R.id.title);
        numview= (TextView) findViewById(R.id.num);
        iconView= (ImageView) findViewById(R.id.icon);
        if (title!=null){
            titleview.setText(title);
        }
        if (num!=null&&num.length()>0){
            numview.setText(num);
        }else{
            numview.setVisibility(View.GONE);
        }
        if (icon!=null){
            iconView.setImageDrawable(icon);
        }
        setClickable(true);
        container=findViewById(R.id.container);
    }

    @Override
    public void setOnClickListener(OnClickListener l) {
        super.setOnClickListener(l);
        container.setOnClickListener(l);
    }
    public void setNum(int num){
        if (num>0){
            numview.setVisibility(View.VISIBLE);
            numview.setText(""+num);
        }else{
            numview.setVisibility(View.GONE);
        }
    }
}
